package com.hgy.commonsale.service.impl;

import java.util.List;

import org.geln.web.hibernate.BaseHibernateService;
import org.geln.web.status.Status;
import com.hgy.commonsale.news.News;
import com.hgy.commonsale.service.NewsService;

import util.CommonUtil;

public class NewsServiceImpl extends BaseHibernateService implements
		NewsService {

	public Status saveObject(News news) {

		if (validateExists(news.getNewsId())) {
			return null;
		}else{
			getHibernateTemplate().save(news);
			return Status.SUCCESS;
		}
	}


	/**
	 * ��֤�Ƿ����
	 * 
	 * @param kindName
	 * @return
	 */
	private Boolean validateExists(String PK) {
		boolean flag = false;
		if (getNewsByPK(PK) != null) {
			flag = true;
		}

		return flag;
	}

	/**
	 * ͨ������PK��ȡ�������ʵ��
	 * 
	 * @param:PK:�������PK
	 * @return:News:�������ʵ��
	 */
	@SuppressWarnings("unchecked")
	public News getNewsByPK(String PK) {

		String strQuery = "from News where newsId=?";
		
		List<News> lstNewss = getHibernateTemplate().find(strQuery,new String[]{PK});

		if (lstNewss!=null && lstNewss.size() > 0) {
			return lstNewss.get(0);
		}

		return null;
	}

	public Status deleteObjectByPK(String PK) {
		News news = getNewsByPK(PK);
		if (CommonUtil.isNotEmpty(news)) {
			getHibernateTemplate().delete(news);
			return Status.SUCCESS;
		}

		return Status.FAILED;

	}

}
